package environment;

import java.util.ArrayList;
import java.util.logging.Logger;

import environment.player.Player;

import ain.ImmuneNetwork;

import util.Vector;

public class Pitch
{
	private double width, height;
	private Ball ball;
	private ArrayList<Player> players; // list iwth all players
	private ArrayList<Vector> startPositions; // default positions of players
	
	private int[] score;
	
	private int counter;
	
	public Pitch(int width, int height)
	{
		this.width = width;
		this.height = height;
		
		score = new int[2];
		
		ball = new Ball(new Vector(width/2, height/2), new Vector(0, 0));
		
		players = new ArrayList<Player>();
	}
	
	public void update()
	{
		for (Player p : players)
		{
			// player decides what to do next
			p.update();
		}
		
		nobodyInBox();
		
		ball.update();
		updateBall();
		
		// eventually used for logging
		counter++;
	}
	
	private void updateBall()
	{		
		// check if out of bounds + check for goal.
		double xPos = ball.getPosition().getX();
		double yPos = ball.getPosition().getY();
		
		checkForGoalBoxScore(xPos, yPos);		
		
		if (xPos < 0 || xPos > width || yPos < 0 || yPos > height)
		{
			ball.setPosition(new Vector(width/2, height/2));
			ball.setVelocity(new Vector());
			
			// put players in initial position and ball on the middle point
			reset();
		}
	}
	
	private void checkForRegularGoals(double xPos, double yPos)
	{
		// update score
		if (xPos < 0 && Math.abs(yPos - height/2) <= 70)
		{
			score[1]++;
		}
		else if (xPos > height && Math.abs(yPos - height/2) <= 70)
		{
			score[0]++;
		}
	}
	
	private void checkForGoalBoxScore(double xPos, double yPos)
	{
		if (yPos > 90 && yPos < height - 90)
		{
			System.out.println("goal");
			// HOME goal
			if (xPos < 60)
			{
				score[1]++;
				reset();
			}
			
			// AWAY goal
			if (xPos > width - 60)
			{
				score[0]++;
				reset();
			}
		}
	}
	
	private void nobodyInBox()
	{
		for (Player p : players)
		{
			if (p.getPosition().getY() > 90 && p.getPosition().getY() < height - 90)
			{				
				if (p.getPosition().getX() < 60 || p.getPosition().getX() > width - 60)
				{
					reset(p);
				}
			}
		}
	}
	
	public void addPlayers(ArrayList<Player> players, ArrayList<Vector> startPositions)
	{
		this.players = players;
		this.startPositions = startPositions;
		
		for (Player p : players)
		{
			// add an Immune system to each player
			p.addImmuneNetwork();
		}
	}
	
	private void reset(Player p)
	{
		if (p.getTeam().equals(Team.HOME))
		{
			p.setPosition(startPositions.get(p.getID()));
		}
	}
	
	public void reset()
	{
		for (int i = 0; i < players.size(); i++)
		{
			players.get(i).setPosition(startPositions.get(i));
			players.get(i).reset();
		}
		
		ball.setPosition(new Vector(width/2, height/2));
		ball.setVelocity(new Vector());
	}
	
	public ArrayList<Player> getPlayers()
	{
		return players;
	}
	
	public Ball getBall()
	{
		return ball;
	}
	
	public double getWidth()
	{
		return width;
	}
	
	public double getHeight()
	{
		return height;
	}
	
	public int[] getScore()
	{
		return score;
	}
}
